import numpy as np
import plotly.graph_objects as go
from plotly.subplots import make_subplots
# Function to generate ellipsoid points
def generate_ellipsoid(a, b, c, num_points=100):
u = np.linspace(0, 2 * np.pi, num_points)
v = np.linspace(0, np.pi, num_points)
x = a * np.outer(np.cos(u), np.sin(v))
y = b * np.outer(np.sin(u), np.sin(v))
z = c * np.outer(np.ones(np.size(u)), np.cos(v))
return x, y, z
# Initial parameters
a, b, c = 1, 1, 1
# Generate initial ellipsoid points
x, y, z = generate_ellipsoid(a, b, c)
# Create the figure
fig = make_subplots(rows=1, cols=1, specs=[[{'type': 'surface'}]])
# Add the initial ellipsoid
fig.add_trace(go.Surface(x=x, y=y, z=z, colorscale='Viridis', showscale=False))
# Set fixed axis limits and aspect ratio
fig.update_layout(
title=f'Ellipsoid: a={a}, b={b}, c={c}',
scene=dict(
xaxis_title='X',
yaxis_title='Y',
zaxis_title='Z',
xaxis=dict(range=[-3, 3]),
yaxis=dict(range=[-3, 3]),
zaxis=dict(range=[-3, 3]),
aspectmode='cube' # Fix aspect ratio
)
)
# Create sliders for semi-major axes a, b, and c
fig.update_layout(
sliders=[
{
'active': 0,
'yanchor': 'top',
'xanchor': 'left',
'currentvalue': {
'prefix': 'Semi-Major Axis a: ',
'visible': True,
'xanchor': 'right'
},
'pad': {'b': 10},
'len': 0.9,
'x': 0.1,
'y': -0.1,
'steps': [{
'label': f'{i/10:.1f}',
'method': 'update',
'args': [{'x': [generate_ellipsoid(i/10, b, c)[0]],
'y': [generate_ellipsoid(i/10, b, c)[1]],
'z': [generate_ellipsoid(i/10, b, c)[2]]},
{'title': f'Ellipsoid: a={i/10:.1f}, b={b}, c={c}'}]
} for i in range(1, 21)] # Steps for semi-major axis a
},
{
'active': 0,
'yanchor': 'top',
'xanchor': 'left',
'currentvalue': {
'prefix': 'Semi-Major Axis b: ',
'visible': True,
'xanchor': 'right'
},
'pad': {'b': 10},
'len': 0.9,
'x': 0.1,
'y': -0.3,
'steps': [{
'label': f'{i/10:.1f}',
'method': 'update',
'args': [{'x': [generate_ellipsoid(a, i/10, c)[0]],
'y': [generate_ellipsoid(a, i/10, c)[1]],
'z': [generate_ellipsoid(a, i/10, c)[2]]},
{'title': f'Ellipsoid: a={a}, b={i/10:.1f}, c={c}'}]
} for i in range(1, 21)] # Steps for semi-major axis b
},
{
'active': 0,
'yanchor': 'top',
'xanchor': 'left',
'currentvalue': {
'prefix': 'Semi-Major Axis c: ',
'visible': True,
'xanchor': 'right'
},
'pad': {'b': 10},
'len': 0.9,
'x': 0.1,
'y': -0.5,
'steps': [{
'label': f'{i/10:.1f}',
'method': 'update',
'args': [{'x': [generate_ellipsoid(a, b, i/10)[0]],
'y': [generate_ellipsoid(a, b, i/10)[1]],
'z': [generate_ellipsoid(a, b, i/10)[2]]},
{'title': f'Ellipsoid: a={a}, b={b}, c={i/10:.1f}'}]
} for i in range(1, 21)] # Steps for semi-major axis c
}
]
)
# Show the figure
fig.show()